package com.wjh.service.impl;

import com.alibaba.fastjson2.JSON;
import com.wjh.base.ResponseApiCode;
import com.wjh.base.ResponseApiData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wjh.feign.IGoodsFeignClient;
import com.wjh.goods.entity.GoodsEntity;
import com.wjh.service.ICommonService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wenjianhai
 * @date 2023/6/26
 * @since JDK 17
 */
@Slf4j
@Service
public class CommonServiceImpl implements ICommonService {

	@Autowired
	private IGoodsFeignClient goodsFeignClient;

	/**
	 * 查询商品详情
	 *
	 * @param goodsGuid 商品Guid
	 * @return GoodsEntity
	 * @date 2023-06-26
	 * @author wenjianhai
	 * @since JDK 17
	 */
	@Override
	public GoodsEntity queryGoods(String goodsGuid) {
		if (StringUtils.isBlank(goodsGuid)) {
			log.info("查询商品详情,商品Guid为空");
			return null;
		}
		ResponseApiData<GoodsEntity> response = goodsFeignClient.queryByGuid(goodsGuid);

		log.info("查询商品详情,商品Guid:{}, 返回结果:{}", goodsGuid, JSON.toJSONString(response));

		if (response == null || !ResponseApiCode.SUCCESS_CODE.equals(response.getCode())) {
			log.info("查询商品详情-失败.商品Guid:{}, 返回结果:{}", goodsGuid, JSON.toJSONString(response));
			throw new RuntimeException("查询商品详情失败");
		}
		return response.getData();
	}
}
